package com.hnhcyy.goods.api;

import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hnhcyy.goods.pojo.Shoppingcart;
import com.hnhcyy.goods.pojo.ShoppingcartQueryParam;
import com.hnhcyy.goods.service.ShoppingcartService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 购物车接口
 * @author sheyongjun  
 * @date 2019年2月16日
 */
@Api(value="/ShoppingcartApi", tags= "购物车接口")
@RestController
@RequestMapping("/shoppingcart")
public class ShoppingcartApi {
	
	@Autowired
	private ShoppingcartService shoppingcartService;

	/**
	 * 加入购物车（单个商品）
	 * @author sheyongjun
	 * @date 2019年2月16日
	 */
	@ApiOperation(value = "加入购物车（单个商品）", notes = "单个商品加入购物车")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "sjid", value = "商家ID", required = true, dataType = "int", paramType = "query"),
		@ApiImplicitParam(name = "spid", value = "商品ID", required = true, dataType = "String", paramType = "query"),
		@ApiImplicitParam(name = "fqty", value = "数量", required = true, dataType = "int", paramType = "query"),
		@ApiImplicitParam(name = "dwbh", value = "客户ID", required = true, dataType = "String", paramType = "query")
	})
	@PostMapping("/add_item")
	public boolean addItem(@RequestParam(defaultValue="1")int sjid, 
						@RequestParam String dwbh,
						@RequestParam String spid, 
						@RequestParam int fqty
						) {
		return shoppingcartService.saveItem(spid, fqty, sjid, dwbh);
	}
	
	/**
	 * 加入购物车（套餐）
	 * @author sheyongjun
	 * @date 2019年2月16日
	 */
	@ApiOperation(value = "加入购物车（套餐）", notes = "套餐加入购物车")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "sjid", value = "商家ID", required = true, dataType = "int", paramType = "query"),
		@ApiImplicitParam(name = "dwbh", value = "客户ID", required = true, dataType = "int", paramType = "query"),
		@ApiImplicitParam(name = "packageId", value = "套餐ID", required = true, dataType = "String", paramType = "query"),
		@ApiImplicitParam(name = "fqty", value = "数量", required = true, dataType = "int", paramType = "query")
	})
	@PostMapping("/add_package")
	public boolean addPackage(@RequestParam(defaultValue="1")int sjid, 
							@RequestParam String dwbh, 
							@RequestParam int packageId, 
							@RequestParam int fqty) {
		return shoppingcartService.savePackage(packageId, fqty, sjid, dwbh);
	}
	
	/**
	 * 获取客户的购物车列表
	 * @author sheyongjun
	 * @date 2019年2月16日
	 */
	@ApiOperation(value = "购物车列表", notes = "获取客户的购物车列表")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "sjid", value = "商家ID", required = true, dataType = "int", paramType = "query"),
		@ApiImplicitParam(name = "dwbh", value = "客户ID", required = true, dataType = "int", paramType = "query"),
	})
	@GetMapping("/list")
	public Map<String,Object> list(@RequestParam(defaultValue="1")int sjid, @RequestParam String dwbh){
		return shoppingcartService.getListByDwbh(sjid, dwbh);
	}
	
	/**
	 * 购物车商品数量加减（有拆零价则要判断价格并修改）
	 * @author sheyongjun
	 * @date 2019年2月16日
	 * 若直接用Shoppingcart作参数，swagger会报错，因为有自定义类型的属性Goods，参考https://hacpai.com/article/1525674135818
	 */
	@ApiOperation(value = "购物车商品数量修改", notes = "购物车商品数量加减")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "fid", value = "购物车ID", required = false, dataType = "int", paramType = "query"),
		@ApiImplicitParam(name = "givefid", value = "套餐ID", required = false, dataType = "int", paramType = "query"),
		@ApiImplicitParam(name = "sjid", value = "商家ID", required = false, dataType = "int", paramType = "query"),
		@ApiImplicitParam(name = "dwbh", value = "客户ID", required = false, dataType = "int", paramType = "query"),
		@ApiImplicitParam(name = "fqty", value = "数量", required = true, dataType = "int", paramType = "query"),
		@ApiImplicitParam(name = "fprice", value = "价格", required = false, dataType = "double", paramType = "query"),
	})
	@PostMapping("/modify")
	public boolean modifyQty(ShoppingcartQueryParam shoppingcartQueryParam) {
		Shoppingcart shoppingcart = new Shoppingcart();
		BeanUtils.copyProperties(shoppingcartQueryParam, shoppingcart);
		return shoppingcartService.updateItem(shoppingcart);
	}
	
	/**
	 * 删除购物车商品
	 * @author sheyongjun
	 * @date 2019年2月17日
	 */
	@ApiOperation(value = "删除购物车商品", notes = "删除购物车商品")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "fids", value = "购物车ID数组", required = true, dataType = "int", paramType = "query"),
	})
	@PostMapping("/delete")
	public boolean delete(@RequestParam int[] fids) {
		return shoppingcartService.removeItem(fids);
	}
}
